package ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.api.ApiConstant;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.PassengerDetailBean;
import com.fy.fyzf.bean.PassengerListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.SearchPassengerBean;
import com.fy.fyzf.presenter.PassengerPresnter;
import com.fy.fyzf.utils.ShareHelper;
import com.fy.fyzf.view.PassengerCallBack;
import com.fy.fyzf.weight.CommonDialog;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import ui.adapter.MatchHousingAdapter;

public class MatchHousingActivity extends BaseActivity<PassengerPresnter> implements PassengerCallBack {


    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    private MatchHousingAdapter matchHousingAdapter;
    private SearchPassengerBean searchPassengerBean;
    private int customerId;
    private View footView;

    @Override
    protected PassengerPresnter createPresenter() {
        return new PassengerPresnter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_macth_housing;
    }

    private int page = 1;
    private int pageSize = 10;
    private int tag = 1;
    @Override
    public void initView() {
        super.initView();
        setTitleName("匹配房源");
        searchPassengerBean = new SearchPassengerBean();

        matchHousingAdapter = new MatchHousingAdapter(this,1,false);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(matchHousingAdapter);
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                tag = 2;
                page++;
                initData();
            }
        });

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                tag = 1;
                initData();

            }
        });

        matchHousingAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<HousesListBean.ListBean> data = adapter.getData();
                String url = ApiConstant.BASE_SERVER_URL + "shareHousing/getHousing?housingId="+data.get(position).getHousingId();

                if (view.getId() == R.id.share_wx) {
                    ShareHelper shareHelper = new ShareHelper(null);
                    shareHelper.shareWx(data.get(position).getHousingImage(), url);
                } else if (view.getId() == R.id.share_pyq) {
                    ShareHelper shareHelper = new ShareHelper(null);
                    shareHelper.shareWechatMoments(data.get(position).getHousingImage(), url);
                }else if(view.getId() == R.id.iv_star){
                    CommonDialog commonDialog = new CommonDialog(MatchHousingActivity.this);
                    if (data.get(position).getCollection() == null) {
                        commonDialog.setContent("是否收藏该房源？");
                    } else {
                        commonDialog.setContent("是否取消收藏该房源？");
                    }
                    commonDialog.toggleDialog();
                    commonDialog.setOnconirmListener(new CommonDialog.OnConfirmClickListener() {
                        @Override
                        public void onClick() {
                            searchPassengerBean.setCustomerId(customerId);
                            searchPassengerBean.setHousingId(data.get(position).getHousingId());
                            if (data.get(position).getCollection() == null) {
                                mPresenter.addCollectHousing(searchPassengerBean,position);
                            } else {
                                mPresenter.removeCollectionHousing(searchPassengerBean,position);
                            }

                            commonDialog.toggleDialog();
                        }
                    });
                }
            }
        });

        footView = LayoutInflater.from(this).inflate(R.layout.layout_foot_view_matchhousing, null);
        matchHousingAdapter.addFooterView(footView);
    }

    @Override
    public void initData() {
        super.initData();
        customerId = getIntent().getIntExtra("id", -1);
        SearchPassengerBean searchPassengerBean = new SearchPassengerBean();
        searchPassengerBean.setPageSize(pageSize);
        searchPassengerBean.setPageNum(page);
        searchPassengerBean.setCustomerId(customerId);
        mPresenter.getRecommendHousing(searchPassengerBean);
    }

    @Override
    public void getPassengerListSuccess(PassengerListBean passengerListBean) {

    }

    @Override
    public void getPassengerDetailSuccess(PassengerDetailBean passengerDetailBean) {

    }

    @Override
    public void getCollectHousingSuccess(HousesListBean housesListBean) {

    }

    HousesListBean housesData;
    @Override
    public void getMatchHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void getRecommendHousingSuccess(HousesListBean housesListBean) {
        smartRefresh.finishLoadMore();
        smartRefresh.finishRefresh();
        housesData = housesListBean;

        if (null == housesListBean || housesListBean.getList().size() <= 0) {
            View view = LayoutInflater.from(this).inflate(R.layout.layout_nodata_matchhousing, null);
            matchHousingAdapter.setEmptyView(view);
            view.findViewById(R.id.tv_look).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MatchHousingActivity.this, AllHousingActivity.class);
                    intent.putExtra("id", customerId);
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }
            });
        }
        if (housesListBean.isIsLastPage()) {

            footView.findViewById(R.id.tv_look).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MatchHousingActivity.this, AllHousingActivity.class);
                    intent.putExtra("id", customerId);
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }
            });
        }
        if (tag == 1) {
            matchHousingAdapter.setNewData(housesListBean.getList());
        } else {
            matchHousingAdapter.addData(housesListBean.getList());
        }
    }

    @Override
    public void collectHousingSuccess(BaseData baseData, int i) {
        housesData.getList().get(i).setCollection("1");
        matchHousingAdapter.notifyItemChanged(i);
    }

    @Override
    public void removecollectHousingSuccess(BaseData baseData, int i) {
        housesData.getList().get(i).setCollection(null);
        matchHousingAdapter.notifyItemChanged(i);
    }

    @Override
    public void AddPassengerSourceSuccess(BaseData baseData) {

    }
    @Override
    public void getSiteListSuccess(List<AreaListBean> areaListBeans) {

    }

    @Override
    public void editPassengerSuccess(BaseData baseData) {

    }

}